package com.surfacing.rcp.model.codec;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;


import org.apache.log4j.Logger;

import com.surfacing.rcp.model.common.AbstractMessage;
import com.surfacing.rcp.model.entity.Login;
import com.surfacing.rcp.model.entity.User;
import com.surfacing.rcp.model.factory.ProtocolCodecFactory;

public class LoginEncoder implements MessageEncoder {
	private static final Logger log = Logger.getLogger(LoginEncoder.class);

	@Override
	public void doEncode(AbstractMessage message, XMLStreamWriter writer)
			throws XMLStreamException {
		// Cast the object to a item
		Login login = (Login) message;

		// assert valid
		if (login == null) {
			log.warn("WARNING: Object login is null and cannot be encoded");
			return;
		}

		// write the start element
		writer.writeStartElement(Login.ID);

		// write the elements and attributes
		writer.writeStartElement("username");
		writer.writeCharacters(login.getUsername());
		writer.writeEndElement();

		if (login.getPassword() != null && !login.getPassword().isEmpty()) {
			writer.writeStartElement("password");
			writer.writeCharacters(login.getPassword());
			writer.writeEndElement();
		}

		// write the elements and attributes
		writer.writeStartElement("loggedIn");
		writer.writeCharacters(String.valueOf(login.isLoggedIn()));
		writer.writeEndElement();

		// write the staff member for this user login
		if (login.getUserInformation() != null) {
			// get the encoder for a staff member
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(User.ID);
			encoder.doEncode(login.getUserInformation(), writer);
		}

		// write the element
		writer.writeStartElement("webClient");
		writer.writeCharacters(String.valueOf(login.isWebClient()));
		writer.writeEndElement();

		// write the elements and attributes
		if (login.getErrorMessage() != null
				&& !login.getErrorMessage().isEmpty()) {
			writer.writeStartElement("errorMessage");
			writer.writeCharacters(login.getErrorMessage());
			writer.writeEndElement();
		}
		// write the elements and attributes
		if (login.getAuthorization() != null
				&& !login.getAuthorization().isEmpty()) {
			writer.writeStartElement("authorization");
			writer.writeCharacters(login.getAuthorization());
			writer.writeEndElement();
		}
		writer.writeStartElement("islocked");
		writer.writeCharacters(String.valueOf(login.isIslocked()));
		writer.writeEndElement();

		// end of the item
		writer.writeEndElement();
	}
}
